import { defineStore } from 'pinia'


export const useBigScreenStore = defineStore('bigScreen', {
  actions: {
    setUserData (obj) {
      this.userData = obj;
    },
    setProjectId (id) {
      if (id) {
        this.projectId = id;
        if (id !== 'all') {
          this.setProject(id);
        }
      } else {
        this.projectId = 0;
      }
    },
    setProject (id) {
      this.project = this.projectList.filter(item => item.id === id)[0]
    },
    calculatedProportion (time) {
      let startTimeList = []
      startTimeList.push(time);
      let maxList = [];
      startTimeList.map(sitem => { maxList.push(new Date(sitem).getTime()) })
      let max = Math.min.apply(null, maxList);
      return ((new Date().getTime() - max) / 1000 / 3600 / 24)
    },
    setProjectList (list) {
      // 先排序
      let arr = list.sort((a, b) => { return b.lightFixtureCount - a.lightFixtureCount; });
      arr.forEach(element => {
        // 设备总功率
        element.sbzgl = parseFloat(element.totalPower * 1 / 1000).toFixed(2)
        // 地面照度
        element.dmzd = parseFloat(element.illuminance * 1).toFixed(2)
        // 运行时长
        let tianshu = this.calculatedProportion(element.projectStartTime || '')
        element.yxsc = parseFloat(tianshu * 1).toFixed(2)
      });
      this.projectList = arr
    },
    setProjectIds (arr) {
      this.projectIds = arr;
    },
    setJsValue (key, value) {
      this.countValue[key] = value;
    },
    setMapLoading (val) { this.isMapLoading = val },
  },
  // 状态
  state () {
    return {
      countValue: {
        sbzgl: 0,
        sbzs: 0,
        dmzd: 0,
        yxsc: 0.
      },
      isHeadShow: true,
      isMenuShow: true,
      // 所有项目
      projectList: [],
      // 所有项目ID
      projectIds: [],
      // 当前项目
      projectId: 0,
      // 当前项目ID
      project: {},
      // 用户信息
      userData: JSON.parse(localStorage.getItem("info")),
      // 地图刷新
      isMapLoading: false
    }
  },
  // 计算
  getters: {}
})